package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.utils.JpushTemplate;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.Coupons;
import org.springblade.modules.app.entity.CouponsConfig;
import org.springblade.modules.app.entity.Jpush;
import org.springblade.modules.app.service.*;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 用户表 控制器
 *
 * @author BladeX
 * @since 2020-10-19
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/usercoupons")
@Api(value = "用户表", tags = "用户表接口")
public class UserCouponsController extends BladeController {

    private final IUserService userMgrService;

    private final ICouponsService couponsService;
    private final ICouponsConfigService couponsConfigService;


    private final ICoalService coalService;
    private final IOrderDetailService orderDetailService;
    private final IUserService userService;


    private final IMoneyDetailService moneyDetailService;

    private final IOrderPoundageService orderPoundageService;

    private final IOrderCommentsService orderCommentsService;

    private final IJpushService jpushService;

    private final IHelpstarService helpstarService;


    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入userMgr")
    public R<User> detail(User userMgr) {
        User detail = userMgrService.getOne(Condition.getQueryWrapper(userMgr));
        return R.data(detail);
    }

    /**
     * 分页 用户表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入userMgr")
    public R<IPage<User>> list(Page page, User params) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        if (!StringUtils.isEmpty(params.getName())) {
            queryWrapper.and(wrapper -> wrapper.eq(User::getName, params.getName()));
        }
        queryWrapper.and(wrapper -> wrapper.eq(User::getShstatus, "2"));
        queryWrapper.and(wrapper -> wrapper.eq(User::getUsertype, "2"));
        queryWrapper.orderByDesc(User::getCreateTime);
        IPage<User> pages = userMgrService.page(page, queryWrapper);
        return R.data(pages);
    }

    /**
     * 自定义分页 用户表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入userMgr")
    public R<IPage<User>> page(Page page, User params) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        if (!StringUtils.isEmpty(params.getName())) {
            queryWrapper.and(wrapper -> wrapper.eq(User::getName, params.getName()));
        }
        queryWrapper.and(wrapper -> wrapper.eq(User::getShstatus, "2"));
        queryWrapper.and(wrapper -> wrapper.eq(User::getUsertype, "2"));
        queryWrapper.orderByDesc(User::getCreateTime);
        IPage<User> pages = userMgrService.page(page, queryWrapper);
        return R.data(pages);
    }

    /**
     * 新增 用户表
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入userMgr")
    public R save(@Valid @RequestBody User userMgr) {
        return R.status(userMgrService.save(userMgr));
    }

    /**
     * 修改 用户表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入userMgr")
    public R update(@Valid @RequestBody User userMgr) {
        return R.status(userMgrService.updateById(userMgr));
    }

    /**
     * 新增或修改 用户表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入userMgr")
    public R submit(@Valid @RequestBody User userMgr) {
        return R.status(userMgrService.saveOrUpdate(userMgr));
    }


    /**
     * 删除 用户表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.YEAR, 1);
        List<Long> idslist = Func.toLongList(ids);
        log.info("====" + ids);
        CouponsConfig couponsConfig = couponsConfigService.getById("1298234754267197442");
//        couponsService.getById()
        for (Long userid : idslist) {
            for (int i = 0; i < couponsConfig.getFfcount(); i++) {
                Coupons coupons = new Coupons();
                coupons.setDelflag(1);
                coupons.setUserid(userid);
                coupons.setTitle(couponsConfig.getTypename());
                coupons.setDiscountprice(couponsConfig.getCouponsmoney());
//                coupons.setExpiredate(cal.getTime());
                coupons.setDelflag(1);
                coupons.setCtime(new Date());
                couponsService.save(coupons);
            }
        }


        List<String> registerIds = new ArrayList<>();
        for (Long userid : idslist) {
            Jpush jpush = jpushService.getById(userid);
            if (!StringUtils.isEmpty(jpush)) {
                registerIds.add(jpush.getRegistrationid());
            }
        }
        JpushTemplate.pushMessage(registerIds, "平台发放海量优惠券 登录平台下单使用吧");


        return R.success("发放成功");
    }


}
